package com.ljd.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ljd.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ljd.gulimall.product.entity.AttrEntity;
import com.ljd.gulimall.product.service.CategoryService;
import com.ljd.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljd.gulimall.product.entity.AttrGroupEntity;
import com.ljd.gulimall.product.service.AttrGroupService;
import com.ljd.common.utils.PageUtils;
import com.ljd.common.utils.R;



/**
 * 属性分组
 *
 * @author ljd
 * @email 2381773911@qq.com
 * @date 2023-02-06 17:20:05
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;
    ///product/attrgroup/{attrgroupId}/attr/relation
    // {catelogId}/withattr
    /**
     * 列表
     */
    @RequestMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrs = attrGroupService.withattr(catelogId);

        return R.ok().put("data", attrs);
    }
    /**
     * 列表
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R list(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrs = attrGroupService.relationAttr(attrgroupId);

        return R.ok().put("data", attrs);
    }

    /**
     * 列表
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noRelationAttr(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils attrs = attrGroupService.noRelationAttr(params, attrgroupId);

        return R.ok().put("page", attrs);
    }

    /**
     * 列表
     */
    @PostMapping("/attr/relation/delete")
    public R list(@RequestBody List<AttrAttrgroupRelationEntity> attrGroups){
       attrGroupService.delRelation(attrGroups);

        return R.ok();
    }

    /**
     * 列表
     */
    @PostMapping("/attr/relation")
    public R relation(@RequestBody List<AttrAttrgroupRelationEntity> attrGroups){
        attrGroupService.relation(attrGroups);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> catIds = categoryService.findParentId(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catIds);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
